import 'package:hive/hive.dart';
import 'package:uuid/uuid.dart';
import '../models/share_comment_model.dart';

class ShareCommentService {
  final Box<ShareCommentModel> _commentBox = Hive.box<ShareCommentModel>(
    'share_comments',
  );
  final Uuid _uuid = Uuid();

  // 添加评论
  Future<ShareCommentModel> addComment({
    required String shareId,
    required String userId,
    required String content,
  }) async {
    final comment = ShareCommentModel(
      id: _uuid.v4(),
      shareId: shareId,
      userId: userId,
      content: content,
      createdAt: DateTime.now(),
    );
    await _commentBox.put(comment.id, comment);
    return comment;
  }

  // 获取某个分享下的所有评论，按时间升序
  List<ShareCommentModel> getCommentsByShareId(String shareId) {
    return _commentBox.values.where((c) => c.shareId == shareId).toList()
      ..sort((a, b) => a.createdAt.compareTo(b.createdAt));
  }

  // 删除评论
  Future<void> deleteShareComment(String commentId) async {
    final comment = _commentBox.get(commentId);
    if (comment != null) {
      await _commentBox.delete(commentId);
    }
  }
}
